<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';


class dao_user {

    //Methods
    public function __construct() {

    }

    public function get_all() {
        $db = new connection();
        $con = $db->open_connect();
        
        $query = "SELECT u.*, c.name AS cinema_name "
                . "FROM tbl_user u "
                . "LEFT JOIN tbl_cinema c ON u.cinema_id = c.id "
                . "WHERE u.role_id = 2 ";
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());
        
        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }

    public function active($id) {
        $db = new connection();
        $con = $db->open_connect();
        $query = "UPDATE tbl_user SET "
                . "status_id = 2 "
                . "WHERE id = " . $id;
//        echo $query;
//        exit();
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }
    
    public function get_by_activate_code($email, $activate_code) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_user "
                . "WHERE role_id = 2 "
                . "AND email = '" . $email . "' "
                . "AND activate_code = '" . $activate_code . "' ";
        
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }
    
    public function save($user_id, $first_name, 
                $last_name, $phone_number, $email, $password, $role_id, 
                $user_cinema_id, $activate_code) {
        $db = new connection();
        $con = $db->open_connect();
        
        if ($user_id == 0) {
            $query = "INSERT INTO tbl_user(email, password, role_id,
                first_name, last_name, phone_number, cinema_id, activate_code) VALUES (
                '" . $email . "','" . md5($password) . "'," . $role_id . ",'" . $first_name . "','" . $last_name . "','" . $phone_number . "','" . $user_cinema_id . "','$activate_code')";
        } else {
            $query = "UPDATE tbl_user SET "
                    . "email = '" . $email . "',"
                    . "password = '" . md5($password) . "',"
                    . "role_id = " . $role_id . ","
                    . "first_name = '" . $first_name . "',"
                    . "last_name = '" . $last_name . "',"
                    . "phone_number = '" . $phone_number . "', "
                    . "cinema_id = " . $user_cinema_id . ", "
                    . "activate_code = '" . $activate_code . "' "
                    . "WHERE id = " . $user_id;
        }
        
        
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $inserted_id = TRUE;
        if ($user_id == 0) {
            $inserted_id = mysqli_insert_id($db->con);
        }
        
        $db->close_connect();
        return $inserted_id;
    }

    public function get_cinema_user($cinema_id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_user "
                . "WHERE role_id = 2 "
                . "AND cinema_id = " . $cinema_id;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }
    
    public function get_by_id($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_user WHERE role_id = 2 AND id = " . $id;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }

    public function delete($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_user WHERE id = " . $id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }

    public function get_by_login_info($email, $password) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_user u JOIN tbl_role r ON u.role_id = r.id "
                . "WHERE email = '" . $email . "' "
                . "AND password = '" . md5($password) . "' "
                . "AND status_id = 2 ";
        $result = mysqli_query($con, $query);
        if (!$result) {
            printf("Error: %s\n", mysqli_error($con));
            exit();
        }
        
        return mysqli_fetch_array($result);
    }
    
    public function get_by_email($email) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_user u JOIN tbl_role r ON u.role_id = r.id "
                . "WHERE email = '" . $email . "'";
        $result = mysqli_query($con, $query);
        if (!$result) {
            printf("Error: %s\n", mysqli_error($con));
            exit();
        }
        
        return mysqli_fetch_array($result);
    }
    
    public function get_first() {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_user "
                . "WHERE role_id = 2 "
                . "LIMIT 0, 1";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }
    
}
